package com.zq.sysuser.config;

import cn.dev33.satoken.listener.SaTokenListener;
import cn.dev33.satoken.stp.SaLoginModel;
import com.zq.sysuser.dao.entity.User;
import com.zq.sysuser.dao.entity.Useractions;
import com.zq.sysuser.service.IUserService;
import com.zq.sysuser.service.IUseractionsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.Date;

/**
 * 自定义侦听器的实现 
 */
@Component
@Slf4j
public class MySaTokenListener implements SaTokenListener {

    @Autowired
    private IUseractionsService useractionsService;

    @Autowired
    private IUserService userService;

    /** 每次登录时触发 */
    @Override
    public void doLogin(String loginType, Object loginId, String tokenValue, SaLoginModel loginModel) {
        System.out.println("---------- 自定义侦听器实现 doLogin");
        Useractions useractions=new Useractions();
        useractions.setUserId((Integer) loginId);
        User user = userService.getById((Serializable) loginId);
        if (user!=null){
            useractions.setUsername(user.getUsername());
            useractions.setActionType("进行登录");
            useractions.setActionTime(LocalDateTime.now());
//            将用户操作记录保存到数据库
            useractionsService.save(useractions);
        }


    }

    /** 每次注销时触发 */
    @Override
    public void doLogout(String loginType, Object loginId, String tokenValue) {
        System.out.println("---------- 自定义侦听器实现 doLogout");
        Useractions useractions=new Useractions();
        useractions.setUserId((Integer) loginId);
        User user = userService.getById((Serializable) loginId);
        if (user!=null){
            useractions.setUsername(user.getUsername());
            useractions.setActionType("退出登录");
            useractions.setActionTime(LocalDateTime.now());
//            将用户操作记录保存到数据库
            useractionsService.save(useractions);
        }


    }

    /** 每次被踢下线时触发 */
    @Override
    public void doKickout(String loginType, Object loginId, String tokenValue) {
        System.out.println("---------- 自定义侦听器实现 doKickout");
    }

    /** 每次被顶下线时触发 */
    @Override
    public void doReplaced(String loginType, Object loginId, String tokenValue) {
        System.out.println("---------- 自定义侦听器实现 doReplaced");
    }

    /** 每次被封禁时触发 */
    @Override
    public void doDisable(String loginType, Object loginId, String service, int level, long disableTime) {
        System.out.println("---------- 自定义侦听器实现 doDisable");
    }

    /** 每次被解封时触发 */
    @Override
    public void doUntieDisable(String loginType, Object loginId, String service) {
        System.out.println("---------- 自定义侦听器实现 doUntieDisable");
    }

    /** 每次二级认证时触发 */
    @Override
    public void doOpenSafe(String loginType, String tokenValue, String service, long safeTime) {
        System.out.println("---------- 自定义侦听器实现 doOpenSafe");
    }

    /** 每次退出二级认证时触发 */
    @Override
    public void doCloseSafe(String loginType, String tokenValue, String service) {
        System.out.println("---------- 自定义侦听器实现 doCloseSafe");
    }

    /** 每次创建Session时触发 */
    @Override
    public void doCreateSession(String id) {
        System.out.println("---------- 自定义侦听器实现 doCreateSession");
    }

    /** 每次注销Session时触发 */
    @Override
    public void doLogoutSession(String id) {
        System.out.println("---------- 自定义侦听器实现 doLogoutSession");
    }
    
    /** 每次Token续期时触发 */
    @Override
    public void doRenewTimeout(String tokenValue, Object loginId, long timeout) {
        System.out.println("---------- 自定义侦听器实现 doRenewTimeout");
    }
}
